import datetime
import urllib

import bot

class TvWeb(object):

    def __init__(self):
        pass

    def obtain_episodes_list(self, serie):
        s = serie.replace(' ', '+')
        search = 'http://www.google.com/search?hl=en&q='+s+'+episode+list+wikipedia'
        robot = bot.Bot(search, serie)
        robot.start()

    def check_for_new_info(self, series, cals, db):
        try:
            r = urllib.urlopen('http://google.com')
            #Check for Series without Calendar
            for ser in series:
                db.delete_calendar(ser)
                self.obtain_episodes_list(ser)
            #Check for New Episodes or Seasons
            date = datetime.date.today()
            serie = ''
            for cal in cals:
                if cal.serie != serie:
                    serie = cal.serie
                    daS = str(cal.date).split('-')
                    da = datetime.date(int(daS[0]), int(daS[1]), int(daS[2]))
                    if date > da:
                        db.delete_calendar(cal.serie)
                        s = cal.serie.replace(' ', '+')
                        search = 'http://www.google.com/search?hl=en&q='+s+'+episode+list+wikipedia'
                        robot = bot.Bot(search, cal.serie)
                        robot._check_new_data = True
                        robot.start()
                        print 'New Season for '+ cal.serie +' checked!'
        except:
            print 'no internet connection detected'
